package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.renren.common.utils.PageUtils;
import io.renren.dao.JxCourseScheduleDao;
import io.renren.entity.JxCourseScheduleEntity;
import io.renren.service.JxCourseScheduleService;
import io.renren.utils.DateTimeUtil;
import io.renren.utils.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("jxCourseScheduleService")
public class JxCourseScheduleServiceImpl extends ServiceImpl<JxCourseScheduleDao, JxCourseScheduleEntity> implements JxCourseScheduleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<JxCourseScheduleEntity> page = this.page(
                new Query<JxCourseScheduleEntity>().getPage(params),
                new QueryWrapper<JxCourseScheduleEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<JxCourseScheduleEntity> getListByPo(Map<String, Object> params) {
        return this.baseMapper.getListByPo( params);
    }

    @Override
    public String ValidatorBegainTime(Date begainTime) {
        String msg="";
        Map<String, Object> params=new HashMap<String, Object>();
        String  courseDay=DateTimeUtil.date(begainTime);
        params.put("courseDay",courseDay);
        List<JxCourseScheduleEntity> list=this.baseMapper.getListByPo( params);
        Integer i=maxTimeStage(list);
        Integer getTimeQuantum=DateTimeUtil.getTimeQuantum(begainTime);
        if(i>getTimeQuantum){
            Integer hour=i/2;
            Integer min=i%2!=0?30:0;
            begainTime.setHours(hour);
            begainTime.setMinutes(min);

         msg="这个时间段已经被预定，请预定"+DateTimeUtil.dateTime(begainTime)+"以后的时间段";
        }

        return msg;
    }

    /**
     * 拿到 所有最大时段
     * @return
     */
    public  Integer  maxTimeStage(List<JxCourseScheduleEntity> list){
        Integer i=0;
        if(list.size()>0){
            for(JxCourseScheduleEntity entity:list){
                String[] t=entity.getTimeStage().split("\\|");
                Integer j=Integer.parseInt(t[t.length-1]);
                if(j>i){
                    i=j;
                }
            }
        }
     return  i;
    }

    /**
     * 拿到最小时段
     * @param list
     * @return
     */
    public Integer  minTimeStage(List<JxCourseScheduleEntity> list){
        Integer i=0;
        String[] t1=list.get(0).getTimeStage().split("\\|");
        i=Integer.parseInt(t1[0]);
        if(list.size()>0){
            for(JxCourseScheduleEntity entity:list){
                String[] t=entity.getTimeStage().split("\\|");
                Integer j=Integer.parseInt(t[0]);
                if(j<i){
                    i=j;
                }
            }
        }
        return  i;
    }

    /***
     * 拿到当天所有时段
     * @return
     */
    public String AllTimeStage(List<JxCourseScheduleEntity> list){
        String txt=list.get(0).getTimeStage();
        if(list.size()>1){
            for(JxCourseScheduleEntity entity:list){
                String[] t=entity.getTimeStage().split("\\|");
                for(String t1:t){
                    if(!txt.contains(t1)){
                        txt=txt+"|"+t1;
                    }
                }

            }
        }
        return  txt;

    }

    /**
     * 验证时间段
     * @param begainTime
     * @param endTime
     * @return
     */
    public String ValidatorTimeStage(Date begainTime,Date endTime){
        String msg="";
        String txt="当天占用时间段分别是：";
        Map<String, Object> params=new HashMap<String, Object>();
        String  courseDay=DateTimeUtil.date(begainTime);
        String timeStage=DateTimeUtil.TimeQuantum(begainTime,endTime);
        String[] timeStages=timeStage.split("\\|");
        params.put("courseDay",courseDay);
        List<JxCourseScheduleEntity> list=this.baseMapper.getListByPo( params);
        String text=AllTimeStage(list);
        for(String t:timeStages){
          if(text.contains(t)){
              msg="时间冲突，";
          }
        }
        for (JxCourseScheduleEntity entity:list){
            txt=txt+"\n"+DateTimeUtil.dateTime(entity.getBegainTime())+"到"+DateTimeUtil.dateTime(entity.getEndTime());
        }
        if(msg.length()>0){
            msg=msg+txt;
        }
      return msg;
    }

    @Override
    public PageUtils getAllWithPage(Map<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("page").toString()),Integer.parseInt(params.get("limit").toString()));
        List<JxCourseScheduleEntity> list = this.baseMapper.getAllWithPage(params);
        PageInfo<JxCourseScheduleEntity> pageInfo = new PageInfo<JxCourseScheduleEntity>(list);
        return new PageUtils(list,(int)pageInfo.getTotal(),pageInfo.getPageSize(),pageInfo.getPageNum());

    }
}
